/*
    jME Terra package. A Terrain Engine for the Java Monkey Engine.
    Copyright (C) 2005-2006,  Tijl Houtbeckers
    
    **** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	**** CONTACT: ****

    I can be reached through the jmonkeyengine.com forums (user "llama"), 
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           

*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.format;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class MemoryMappedFileMapStore extends FileMapStore {

	private boolean useTempFile;
	public MemoryMappedFileMapStore(String filename, boolean includeCoordinates, boolean useTempFile, boolean readOnly) {
		super(filename, includeCoordinates);
		init(useTempFile, readOnly);
	}

	public MemoryMappedFileMapStore(String filename, boolean includeCoordinates, String extention, boolean useTempFile, boolean readOnly) {
		super(filename, includeCoordinates, extention);
		init(useTempFile, readOnly);
	}
	
	private String filemode;
	private MapMode mapmode;
	
	private void init(boolean useTempFile, boolean readOnly) {
		this.useTempFile = useTempFile;
		if (readOnly) {
			filemode="r";
			mapmode = MapMode.READ_ONLY;
		}
		else {
			filemode="rw";
			mapmode = MapMode.READ_WRITE;
		}
		
	}


	public ByteBuffer loadMap(int x, int y) throws IOException {
		File file = getFile(x, y, false);
		return mapFile(file, -1);
	}
	
	protected ByteBuffer mapFile(File file, int length) throws IOException {
		RandomAccessFile raf = new RandomAccessFile(file, filemode);
		length = prepFile(raf, length);
		FileChannel channel = raf.getChannel();
		ByteBuffer ret = channel.map(mapmode,4,length);
		channel.close();
		raf.close();
		return ret;
	}
			
	
	protected int prepFile(RandomAccessFile raf, int size) throws IOException {
		if (raf.length()-4 < size) {
			raf.setLength(4+size);
			raf.seek(0);
			raf.writeInt(size);
		}
		else if (raf.length()-4 > size) {
			raf.seek(0);
			return raf.readInt();
		}
		return size;
	}
	
	
	protected File getFile(int x, int y, boolean create) throws IOException {
		File file = super.getFile(x, y, create);	
		if (useTempFile) {
			file.deleteOnExit();
		}
		return file;

	}

	public ByteBuffer createMap(int x, int y, int size) throws IOException {		
		File file = getFile(x, y, true);
		return mapFile(file, size);		
	}

	public void saveMap(int x, int y, ByteBuffer buf) throws IOException {
		((MappedByteBuffer) buf).force();				
	}

}
